package com.ruoyi.activiti.config;

import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.service.ISysRoleService;
import com.ruoyi.system.service.ISysUserService;
import org.activiti.api.runtime.shared.identity.UserGroupManager;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author danghf
 * @date 2024/04/28 18:32:47
 * 用户组管理器
 */
@Service
@Primary
public class IUserGroupManager implements UserGroupManager {

    private final ISysRoleService roleService;

    private final ISysUserService userService;

    public IUserGroupManager(ISysRoleService roleService, ISysUserService userService) {
        this.roleService = roleService;
        this.userService = userService;
    }

    @Override
    public List<String> getUserGroups(String username) {
        return getUserRoles(username);
    }

    @Override
    public List<String> getUserRoles(String username) {
        Long userId = SecurityUtils.getUserId();
        return roleService.selectRolesByUserId(userId)
                .stream().map(role -> String.valueOf(role.getRoleId()))
                .collect(Collectors.toList());
    }

    @Override
    public List<String> getGroups() {
        return roleService.selectRoleAll()
                .stream().map(role -> String.valueOf(role.getRoleId()))
                .collect(Collectors.toList());
    }

    @Override
    public List<String> getUsers() {
        return userService.selectUserList(null)
                .stream().map(user -> String.valueOf(user.getUserId()))
                .collect(Collectors.toList());
    }
}
